package com.yh.dao.impl;

import com.yh.dao.JDBCUtil;
import com.yh.dao.UserDao;
import com.yh.dao.common.HibernateUtil;
import com.yh.model.UserModel;
import org.hibernate.Hibernate;
import org.hibernate.Query;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoimpl implements UserDao {
    private JDBCUtil jdbc = new JDBCUtil();
    @Override
    public List<UserModel> login(String phone, String pwd) {
//        List<UserEntity> list = new ArrayList<>();
//        try {
//            ResultSet result  =jdbc.Find("select * from user where uphone = ? and upwd = ?",phone,pwd);
//            while(result.next()){
//                list.add(new UserEntity(result.getInt(1),result.getString(2),result.getString(3),
//                        result.getString(4),result.getString(5),result.getString(6)));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return list.size();
        Query query = HibernateUtil.currentSession().createQuery("from UserModel where uphone = ? and upwd = ?");
        query.setParameter(0,phone);
        query.setParameter(1,pwd);
        List<UserModel> list =  query.list();
        return list;
    }

    @Override
    public int regist( String name, String pwd, String phone, String rename) {
//        int a = -1;
//        try {
//            a = jdbc.ExcuteUpdate("insert into user (uid, uname, upwd, uphone, `rename`) VALUES (null,?,?,?,?)",name,pwd,phone,rename);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        Query query = HibernateUtil.currentSession().createSQLQuery("insert into user (uid, uname, upwd, uphone, `rename`) VALUES (null,?,?,?,?)");
        query.setParameter(0,name).setParameter(1,pwd).setParameter(2,phone).setParameter(3,rename);
        int a = query.executeUpdate();
        return a;
    }

    @Override
    public String isAdmin(String phone, String pwd) {
        String isno = "";
//        try {
//            isno = jdbc.FindOne("select isAdmin from user where uphone = ? and upwd = ?",String.class,phone,pwd);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        Query query = HibernateUtil.currentSession().createQuery("from UserModel where uphone = ? and upwd = ?");
        query.setParameter(0,phone);
        query.setParameter(1,pwd);
        List<UserModel> list =  query.list();
        isno = list.get(0).getIsAdmin();
        return isno;
    }
}
